//
//  BaseViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "BaseViewController.h"
#import "SharedViewController.h"

#import <UMSocialCore/UMSocialCore.h>
#import <UMSocialUIManager.h>

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = WHITE_COLOR;
    self.navigationController.navigationBar.barTintColor = kThemeColor;
    
    [self setUpLeftButtonItem];
}

/*
 *  默认修改了左上角的item值
 */
- (void)setUpLeftButtonItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}

/*
 *  如果想任意跳转，就重写goBack方法
 */
- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//  ***********************【 侧滑 】**********************
/** 中间的nav */
//所有左侧控制器共有方法 定位唯一导航控制器
- (UINavigationController *)centerNavigationController{
    
    return (UINavigationController *)self.sidePanelController.centerPanel;
}

//所有左侧控制器共有方法 切换中心控制器
- (void)jumpToOtherViewController:(UIViewController *)otherVC{
    
    
    //分享控制器modal出来
    if ([otherVC isKindOfClass:[SharedViewController class]]) {
        
        __weak typeof(self) weakSelf = self;
        
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMShareMenuSelectionView *shareSelectionView, UMSocialPlatformType platformType) {
            
//            [weakSelf shareWithPlatfromType:platformType];
            [weakSelf shareTextToPlatformType:platformType];
        }];
        
        return;
    }
    
    //显示中间的panel
    [self.sidePanelController showCenterPanelAnimated:YES];
    
    //push到指定的VC
    [[self centerNavigationController] pushViewController:otherVC animated:NO];
    //用yes 双重动画 不好看
}




#pragma mark :-分享内容
//分享文本
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = @"IOS大神们";
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                NSLog(@"response message is %@",resp.message);
                //第三方原始返回的数据
                NSLog(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                NSLog(@"response data is %@",data);
            }
        }
        
        //        //回到应用移除背景灰色
        //        [_presentVc tapClick];
        
        [self alertWithError:error];
    }];
}
//弹出框提示
- (void)alertWithError:(NSError *)error {
    NSString *result = nil;
    if (!error) {
        result = [NSString stringWithFormat:@"恭喜你,分享成功!邀请好友一起玩转!"];
    }
    else{
        if (error) {
            result = [NSString stringWithFormat:@"分享失败,此功能正在努力开通中...code: %d\n",(int)error.code];
        }
        else{
            result = [NSString stringWithFormat:@"额,分享失败了!"];
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"越分享,越快乐!" message:result delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", @"确定") otherButtonTitles:nil];
    
    [alert show];
    
    
}




-(void)shareWithPlatfromType:(UMSocialPlatformType)platformType{
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    NSString *title = @"黑马";
    
    NSString *desc = @"我是大神";
    
    NSString *webpageUrl = @"http://www.baidu.com";
    
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:desc thumImage:[UIImage imageNamed:@"jinqi"]];
    
    //设置网页地址
    shareObject.webpageUrl = webpageUrl;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享借口
    [[UMSocialManager defaultManager]shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
        
        if (error) {
            
            NSLog(@"分享失败%@",error);
        }else{
            
            if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                
                UMSocialShareResponse *resp = result;
                
                //分享结果消息
                NSLog(@"%@",resp.message);
                //从第三方原始返回的数据
                NSLog(@"%@",resp.originalResponse);
                
            }else{
                
                NSLog(@"%@",result);
                
            }
            
        }
        
        [self alertWithError:error];
        
    }];
    
    
}


@end
